package com.tesis.constdroid;

import java.util.Vector;





import android.content.res.Resources;
import android.widget.TextView;
import choco.cp.model.CPModel;
import choco.cp.solver.CPSolver;
import choco.cp.solver.search.integer.valselector.RandomIntValSelector;
import choco.kernel.model.Model;
import choco.kernel.model.variables.integer.IntegerVariable;
import choco.kernel.solver.Solver;

public class Solve {
	
	Resources res;
	Model model=null;
	Solver solver;
	Constraints constraints;
	Vector<IntegerVariable> varArray;
	int nSol=1;
	
	public Solve(Resources r){
		res = r;
		model = null;
	    solver = null;
	}
	
	public  void setValues(Constraints con,Vector<IntegerVariable> varArray ){
		this.constraints=con;
		this.varArray= varArray;
	}
	
	public void solve(TextView t){
		
		if(model==null){
			
			model = new CPModel();
			solver = new CPSolver();
			
			for(int i = 0;i< constraints.getConts().size();i++){
		    	model.addConstraint(constraints.getConts().get(i));
		    }
			
			
			
			solver.read(model);
			
			solver.setValIntSelector(new RandomIntValSelector());
			if(solver.solve()==true){
				String text=res.getString(R.string.solution_number)+ " " + nSol + "\n";
	        	nSol= nSol + 1;
	        	for(int i =0 ; i < varArray.size();i++){
	        		
	        		text=text + solver.getVar(varArray.get(i)) + "  ";
	        		
	        	}
	        	t.setText(text);
	        	return;
			}else{
				model=null;
				
				t.setText(res.getString(R.string.no_solution));
				return;
			}
		}
		
		if(solver.nextSolution()==true){
			String text=res.getString(R.string.solution_number)+ " " + nSol + "\n";
        	nSol= nSol + 1;
        	for(int i =0 ; i < varArray.size();i++){
        		
        		text=text + solver.getVar(varArray.get(i)) + "  ";
        		
        	}
        	t.setText(text);
        }else{
        	t.setText(res.getString(R.string.no_more_solutions));
        	model=null;
        	nSol=0;
        }
	}
	
	public void solveAll(TextView t){
		nSol=0;
		model = new CPModel();
		solver = new CPSolver();
		
		for(int i = 0;i< constraints.getConts().size();i++){
	    	model.addConstraint(constraints.getConts().get(i));
	    }
		solver.read(model);
		solver.setValIntSelector(new RandomIntValSelector());
		if(solver.solve()==true){
			
        	String text="Solution number " + nSol + "\n";
        	nSol= nSol + 1;
        	for(int i =0 ; i < varArray.size();i++){
        		
        		text=text + solver.getVar(varArray.get(i)) + "  ";
        		
        	}
        	text = text + "\n";
        	while(solver.nextSolution()==true){
    			
            	text= text + res.getString(R.string.solution_number)+ " " + nSol + "\n";
            	nSol= nSol + 1;
            	for(int i =0 ; i < varArray.size();i++){
            		text=text + solver.getVar(varArray.get(i)) + "  ";
            	}
            	text = text + "\n";
            }
        	t.setText(text);
        	
		}else t.setText(res.getString(R.string.no_solution));
		model=null;
    	nSol=0;
        
		
	}
}
